package com.bnyy.medicalHousekeeper.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ScreenUtils;
import com.bnyy.gbp.permission.PermissionHelper;
import com.bnyy.medicalHousekeeper.DialogHelper;
import com.bnyy.medicalHousekeeper.GlideHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.adapter.ShopServiceGoodsAdapter;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.bean.ServiceGoods;
import com.bnyy.medicalHousekeeper.bean.ShopInfo;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.listener.OnPageChangeListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.function.Consumer;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/9/29
 **/
public class ShopActivity extends BaseActivityImpl {
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.tv_indicator)
    TextView tvIndicator;
    @BindView(R.id.tv_shop_name)
    TextView tvShopName;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_business_hours)
    TextView tvBusinessHours;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.iv_no_data)
    ImageView ivNoData;
    @BindView(R.id.tv_no_data)
    TextView tvNoData;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.refresh_layout)
    RelativeLayout refreshLayout;
    @BindView(R.id.tv_check_all)
    TextView tvCheckAll;
    @BindView(R.id.tv_type)
    TextView tvType;

    public static void show(Context context, int shopId) {
        Intent intent = new Intent(context, ShopActivity.class);
        intent.putExtra("shopId", shopId);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_service_shop;
    }

    @Override
    public String getTitleStr() {
        return " ";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int shopId = getIntent().getIntExtra("shopId", -1);
        if (shopId == -1) {
            finish();
        } else {
            getShopInfo(shopId);
        }
    }

    private void getShopInfo(int shopId) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("id", shopId);
        requestManager.request(requestManager.mJavaRetrofitService.getShop(RequestManager.getJsonRequestBody(params)),
                new BaseObserverImpl<ShopInfo>(mContext) {
                    @Override
                    public void onSuccess(ShopInfo shopInfo) {
                        super.onSuccess(shopInfo);

                        getServiceGoodsList(shopId);

                        tvType.setTextColor(Color.parseColor("#609AFF"));
                        tvType.setText(shopInfo.getType_name());
                        GradientDrawable bg = new GradientDrawable();
                        bg.setCornerRadius(10);
                        bg.setColor(Color.parseColor("#E8F0FF"));
                        tvType.setBackground(bg);

                        tvShopName.setText(shopInfo.getName());
                        setTitleStr(shopInfo.getName());
                        tvAddress.setText(shopInfo.getAddr());
                        tvBusinessHours.setText(shopInfo.getBusiness_desc());
                        String phone = shopInfo.getPhone();
                        setBanner(shopInfo.getImgs());
                    }
                });
    }

    private void getServiceGoodsList(int shopId) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("id", shopId);
        params.put("page_index", 1);
        params.put("page_size", 10);
        requestManager.request(requestManager.mJavaRetrofitService.getShopServiceGoodsList(RequestManager.getJsonRequestBody(params)),
                new BaseObserverImpl<ArrayList<ServiceGoods>>() {
                    @Override
                    public void onSuccess(ArrayList<ServiceGoods> serviceGoods) {
                        super.onSuccess(serviceGoods);
                        int size = serviceGoods.size();
                        if (size > 0) {
                            int showCount = 6;
                            ArrayList<ServiceGoods> target = new ArrayList<>();
                            if (size > showCount) {
                                for (int i = 0; i < showCount; i++) {
                                    target.add(serviceGoods.get(i));
                                }
                                tvCheckAll.setVisibility(View.VISIBLE);
                                tvCheckAll.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        ShopServiceGoodsListActivity.show(mContext, shopId);
                                    }
                                });
                            } else {
                                target.addAll(serviceGoods);
                            }
                            ShopServiceGoodsAdapter adapter = new ShopServiceGoodsAdapter(mContext);
                            recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
                            recyclerView.setAdapter(adapter);
                            adapter.refresh(target);
                        } else {
                            llNoData.setVisibility(View.VISIBLE);
                        }
                    }
                });
    }

    private void setBanner(ArrayList<String> goodsImages) {
        if (goodsImages != null && goodsImages.size() > 0) {
            int width = ScreenUtils.getScreenWidth();
            banner.setLayoutParams(new RelativeLayout.LayoutParams(width, width / 15 * 13));
            banner.setAdapter(new BannerImageAdapter<String>(goodsImages) {
                @Override
                public void onBindView(BannerImageHolder holder, String data, int position, int size) {
                    Glide.with(mContext).load(GlideHelper.getGlideUrl(data)).centerCrop().into(holder.imageView);
                }
            });

            banner.isAutoLoop(false);

            GradientDrawable bg = new GradientDrawable();
            bg.setColor(Color.parseColor("#40000000"));
            bg.setCornerRadius(360);

            tvIndicator.setBackground(bg);
            tvIndicator.setTextColor(Color.WHITE);
            tvIndicator.setText("1 / " + goodsImages.size());

            banner.addOnPageChangeListener(new OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    tvIndicator.setText((position + 1) + " / " + goodsImages.size());
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        }
    }
}
